﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.UI.Controllers;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.Services;

namespace MethodWorx.CMS.UI.Tests
{
    [TestClass]
    public class ConfigurePageTemplateControllerTests
    {
        public readonly Guid PAGETEMPLATE_ID_1 = new Guid("{BD03BABC-467E-4D31-AFA6-C2E5779074A3}");
        public readonly Guid PAGETEMPLATE_ID_2 = new Guid("{BD03BABC-467E-4D31-AFA6-C2E5779074A4}");

        private ConfigurePageTemplateController CreateSystemUnderTest()
        {
            PageTemplateRepository ptr = new PageTemplateRepository(
                new PageTemplate[]
                {
                    new PageTemplate() { Id = PAGETEMPLATE_ID_1, Name = "Child", Abstract = true },
                    new PageTemplate() { Id = PAGETEMPLATE_ID_2, Name = "Master", Abstract = true }
                }.AsQueryable());

            return new ConfigurePageTemplateController(new SchemaService(new StubUnitOfWorkFactory(), ptr, new ContentTemplateRepository(new ContentTemplate[] { }.AsQueryable())), null, null, null);
        }

        [TestMethod]
        public void MasterSelectListShouldNotContainSelf()
        {
            var controller = this.CreateSystemUnderTest();
            ViewResult result = controller.BasicDetails(PAGETEMPLATE_ID_1) as ViewResult;

            Assert.IsNotNull(result);
            var items = ((ConfigurePageTemplateModel)result.ViewData.Model).PageTemplateBasicDetails.ParentsSelectList.Items.Cast<PageTemplate>();

            Assert.IsFalse(items.Where(i => i.Id == PAGETEMPLATE_ID_1).Any());
        }

        [TestMethod]
        public void MasterSelectListShouldNotAbstractPageTemplates()
        {
            var controller = this.CreateSystemUnderTest();
            ViewResult result = controller.BasicDetails(PAGETEMPLATE_ID_1) as ViewResult;

            Assert.IsNotNull(result);
            var items = ((ConfigurePageTemplateModel)result.ViewData.Model).PageTemplateBasicDetails.ParentsSelectList.Items.Cast<PageTemplate>();

            Assert.IsTrue(items.Where(i => i.Id == PAGETEMPLATE_ID_2).Any());
        }
    }
}
